// pages/group/publish/publish.js
const qiniuUploader = require("../../../utils/qiniuUploader");
const util = require('../../../utils/util');
const app = getApp();
import {
    _post,
    _headerGet,
    _headerPost,
    config
} from '../../../utils/httpUtils.js'
Page({
    data: {
        navbarData: {
            showCapsule: 1, //是否显示左上角图标
            title: '分享中' //导航栏 中间的标题
        },
        height: app.globalData.height * 2 + 20,
        windowHeight: '', //页面高度
        QiNiuToken: '',
        allImages: [],
        address: false,
        date: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        wx.getSystemInfo({ //获取系统信息
            success: (res) => {
                this.setData({
                    windowHeight: res.screenHeight - this.data.height
                })
            }
        })
        this.data.date = util.formatTime(new Date()).substring(0, 10) // 设置时间字符串

        let identifier = options.identifier;
        this.setData({
            identifier: options.identifier
        })

        if (identifier != 2) {
            this.getDynamicCate(identifier);
        }
    },

    getLocation() {
        // wx.getLocation({
        //     type: 'gcj02', // 返回可以用于wx.openLocation的经纬度
        //     success(res) {
        //         const latitude = res.latitude
        //         const longitude = res.longitude
        //         wx.openLocation({
        //             latitude,
        //             longitude,
        //             scale: 18
        //         })
        //     }
        // })
        let that = this;
        wx.chooseLocation({
            success(res) {
                console.log(res)
                that.setData({
                    address: res
                })
            }
        })
    },
    formSubmit(e) { // 发布事件
        let subData = e.detail.value;
        if (subData.title.length == '') {
            wx.showToast({
                title: '请输入发布标题',
                duration: 2000,
                icon: 'none'
            })
            return false
        }
        if (subData.da_content.length == '') {
            wx.showToast({
                title: '请输入发布内容',
                duration: 2000,
                icon: 'none'
            })
            return false
        }
        if (this.data.identifier == 2 || this.data.identifier == 1) {
            subData.topic_id = this.data.identifier;
        } else if (this.data.cate_id == undefined) {
            wx.showToast({
                title: '请选择话题',
                duration: 2000,
                icon: 'none'
            })
            return false
        }

        subData.client = 'weixinSmall';
        // subData.images = this.data.allImages;
        subData.images = JSON.stringify(this.data.allImages);
        subData.dynamic_cate_id = this.data.cate_id;
        console.log(subData)
        _headerPost('/api/dynamic/dynamic_add', subData, res => {
            console.log(res.data)
            if (res.data.code == 1) {
                wx.showModal({
                    content: `${res.data.msg}是否返回?`,
                    icon: 'success',
                    success: userRes => {
                        if (userRes.confirm) {
                            wx.navigateBack({
                                delta: 1
                            })
                        }

                    }
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    duration: 2000,
                    icon: 'none'
                })
            }
        })
    },
    didPressChooesImage() { //选择照片
        this.getQiniuToken() //
        let that = this;
        // return
        wx.chooseImage({
            count: 9,
            success: res => {
                this.setData({
                    src: res.tempFilePaths
                })
                // 循环上传
                for (let i = 0; i < res.tempFilePaths.length; i++) {
                    let filePath = res.tempFilePaths[i];
                    // let random = Math.floor(Math.random() * 999999);
                    let uploadKey = (util.formatTime(new Date()) + Math.floor(Math.random() * 99999) + i).replace(/\-/g, '')
                    // 上传七牛云
                    qiniuUploader.upload(filePath, (res) => {
                            this.data.allImages.push({
                                "image_path": res.file_path,
                                "status": res.code
                            })
                        }, (error) => {
                            console.log('error: ' + error);
                        }, {
                            region: 'NCN', //區域代碼 https://up-z1.qbox.me
                            key: `${this.data.date}/${uploadKey}.png`, //文件命名
                            uptoken: this.data.QiNiuToken,
                            domain: 'http://qn.cgxczx.com',
                            shouldUseQiniuFileName: false
                        },
                        (res) => {
                            console.log('上传进度', res.progress)
                        }, null
                    );
                }
            }
        })
    },
    getQiniuToken() { //获取七牛云token
        _headerGet('/api/upload/getQiniuToken', {}, res => {
            console.log(res.data.data.QiNiuToken)
            if (res.data.code == 1) {
                this.data.QiNiuToken = res.data.data.QiNiuToken
            }
            // let options = {
            //     region: 'NCN',
            //     key: `${this.data.date}'/'${this.data.dateTime}.png`,
            //     uptoken: res.data.data.QiNiuToken,
            //     domain: 'http://pjb49cah9.bkt.clouddn.com',
            //     shouldUseQiniuFileName: false
            // };
            // qiniuUploader.init(options);
        })
    },
    getDynamicCate(topic_id) { //获取动态话题 id
        _headerGet('/api/dynamic/getDynamicCate', {
            'topic_id': topic_id || 0
        }, res => {
            if (res.data.code == 1) {
                this.setData({
                    dynamicCate: res.data.data
                })
            }
        })
    },
    menuClick(e) { //话题菜单选择
        this.setData({
            cate_id: e.target.dataset.cate_id
        })
    }
})